package com.android.djlmapp.adapter.kaika;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.activity.kaika.web.TcDetailsActivity;
import com.android.djlmapp.model.kaika.SelectTcInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by PVer on 2018/1/17.
 */

public class SelectTcAdapter extends BaseExpandableListAdapter implements ExpandableListAdapter {

    private List<SelectTcInfo.SpecList> mGoups = new ArrayList<>();
    private Context mContext;
    private Activity mActivity;

    public SelectTcAdapter(Context context) {
        this.mContext = context;
        this.mActivity = (Activity) context;
    }

    public void setData(List<SelectTcInfo.SpecList> groups) {
        this.mGoups = groups;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        if (mGoups != null && mGoups.size() > 0) {
            return mGoups.size();
        } else {
            return 0;
        }
    }

    @Override
    public int getChildrenCount(int groupPostion) {
        if (mGoups != null && mGoups.size() > 0) {
            SelectTcInfo.SpecList specList = mGoups.get(groupPostion);
            ArrayList<SelectTcInfo.SpecList.Sys> syses = specList.getSys();

            if (syses != null && syses.size() > 0) {
                int size = syses.size();
                return size;
            } else {
                return 0;
            }
        } else {
            return 0;
        }

    }

    @Override
    public SelectTcInfo.SpecList getGroup(int groupPosition) {
        return mGoups.get(groupPosition);
    }

    @Override
    public SelectTcInfo.SpecList.Sys getChild(int groupPosition, int childPosition) {
        return mGoups.get(groupPosition).getSys().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //子目录实现点击事件必须要在适配器中isChildSelectable方法返回true，否则无法实现
    //子目录的布局里面的顶层Layout设置属性：android:descendantFocusability="blocksDescendants"
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder holder;
        if (convertView == null) {
            holder = new GroupViewHolder();
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_selecttc_group, parent, false);
            holder.mGroupName = convertView.findViewById(R.id.tv_tc_groupname);

            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        holder.mGroupName.setText(mGoups.get(groupPosition).getSpec_1());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        final SelectTcInfo.SpecList.Sys childInfo = mGoups.get(groupPosition).getSys().get(childPosition);
        ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_selecttc_child, parent, false);
            holder.mRlFgx = convertView.findViewById(R.id.rl_fgx);
            holder.fengexian = convertView.findViewById(R.id.fengexian);
            holder.mChildName = convertView.findViewById(R.id.tv_tc_childname);
            holder.mChildPackagePrice = convertView.findViewById(R.id.tv_tc_price);
            holder.mIvGoXq = convertView.findViewById(R.id.iv_go_tcxq);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }

        if (childPosition == 0) {
            holder.mRlFgx.setVisibility(View.VISIBLE);
        } else {
            holder.mRlFgx.setVisibility(View.GONE);
        }

        if (childPosition == getChildrenCount(groupPosition) - 1) {
            holder.fengexian.setVisibility(View.GONE);
        } else {
            holder.fengexian.setVisibility(View.VISIBLE);
        }

        holder.mChildName.setText(childInfo.getSpec_name());

        holder.mIvGoXq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 跳到详情页
                mActivity.startActivity(new Intent(mActivity, TcDetailsActivity.class).
                        putExtra("SPCID", childInfo.getSpec_id1()));
            }
        });

        return convertView;
    }

    static class GroupViewHolder {
        //套餐组名称
        public TextView mGroupName;

    }

    static class ChildViewHolder {
        // 父类分割线
        public RelativeLayout mRlFgx;
        // 子类分割线
        public View fengexian;
        //子套餐名称
        public TextView mChildName;
        //子套餐价格
        public TextView mChildPackagePrice;

        // 跳转详情
        public ImageView mIvGoXq;
    }
}
